﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Validation;

namespace nHydrate.Dsl
{
	[ValidationState(ValidationState.Enabled)]
	partial class SelectCommand
	{
		[ValidationMethod(ValidationCategories.Open | ValidationCategories.Save | ValidationCategories.Menu | ValidationCategories.Custom | ValidationCategories.Load)]
		public void Validate(ValidationContext context)
		{
			if (!this.IsGenerated) return;
			
			System.Windows.Forms.Application.DoEvents();
			var timer = nHydrate.Dsl.Custom.DebugHelper.StartTimer();
			try
			{
				#region Check valid name
				if (!ValidationHelper.ValidDatabaseIdenitifer(this.DatabaseName))
					context.LogError(string.Format(ValidationHelper.ErrorTextInvalidIdentifierSelectCommand, this.Name, this.Entity.Name), string.Empty, this);
				else if (!ValidationHelper.ValidCodeIdentifier(this.PascalName))
					context.LogError(string.Format(ValidationHelper.ErrorTextInvalidIdentifierSelectCommand, this.Name, this.Entity.Name), string.Empty, this);
				#endregion

				#region Check parameters
				if (this.Parameters.Count(x => x.IsGenerated) == 0)
					context.LogWarning(string.Format(ValidationHelper.WarningTextSelectCommandNeedParameters, this.Entity.Name, this.Name), string.Empty, this);
				#endregion

				#region Check SQL
				if (string.IsNullOrEmpty(this.SQL))
					context.LogError(string.Format(ValidationHelper.ErrorTextSQLRequiredSelectCommand, this.Name), string.Empty, this);
				#endregion

			}
			catch (Exception ex)
			{
				throw;
			}
			finally
			{
				nHydrate.Dsl.Custom.DebugHelper.StopTimer(timer, "Select Command Validate - Main");
			}

		}
	}
}
